import { getAppFun } from './app.js'
import { BrowserInfo } from './BrowserInfo.js'
let appFun = getAppFun()
let Browser = BrowserInfo()
export const getFun = () => {
	let obj = {
		computed: {
			clientHeight: function () {
				return (document.documentElement.clientHeight || document.body.clientHeight) - 44
			},
			clientWidth: function () {
				return document.documentElement.clientWidth || document.body.clientWidth
			},			
			isOpenInApp: function () {
				// 判断是否在IOS环境下
				var ua = navigator.userAgent.toLowerCase()
				// 如果实在苹果的浏览器里就不显示 否则就是在苹果的app里就显示  这边容易写反
				if (ua.match(/(iPhone|iPod|iPad);?/i)) {
					if (ua.match(/safari/i)) {
						return false
					} else if (ua.match(/MicroMessenger/i) == 'micromessenger') {
						return false
					} else {
						return true
					}
				}
				// 判断是否在安卓环境下
				if (ua.match(/android/i)) {
					try {
						window.loadAndroid.readPackage()
						return true
					}
					catch (err) {
						return false
					}
				}
			}
		},
		watch: {
		  $route: {
		    handler: function(val, oldVal){

		    },
		    // 深度观察监听
		    deep: true
		  }
		},				
		mounted() {
			this.userId = this.$router.currentRoute.query.userId;
		},
		methods: {
			...appFun,
			IsURL(str_url) {
				var reg = /^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\*\+,;=.]+$/;
				if (reg.test(str_url)) {
					return true;
				} else {
					return false;
				}
			},

			// 转化电话号码中间时*
			formatTelphone(str) {
				if (str) {
					let str1 = str.substring(0, 3)
					let str2 = str.substring(7, 11)
					return str1 + "****" + str2
				}
			},
			// 转化成数组
			formatArr(str) {
				if (str) {
					return str.split(',')
				} else {
					return []
				}
			},
			// 转化2位小数 必须确保是个数字
			formatFixed2(num) {
				if (num) {
					return num.toFixed(2)
				} else {
					return '0.00'
				}
			},
			formatOrderStatus(num) {
				let str
				switch (num) {
					case 0:
						str = '卖家已关闭'
						break;
					case 1:
						str = '已拍下'
						break;
					case 2:
						str = '已付款'
						break;
					case 3:
						str = '已发货'
						break;
					case 4:
						str = '交易完成(已收货)'
						break;
					case 5:
						str = '退款中'
						break;
					case 6:
						str = '已评价'
						break;
					case 7:
						str = '完成'
						break;
					case 8:
						str = '已退款'
						break;
					case 9:
						str = '自动结束'
						break;
					case 10:
						str = '(买家)已关闭'
						break;
					case 11:
						str = '已删除'
						break;
					case 12:
						str = '交易自动结束'
						break;
					case 13:
						str = '交易完成'
						break;						
				}
				return str
			},
			// 转化退款类型
			formatRefundReason(num) {
				let str
				switch (num) {
					case 1:
						str = '仅退款'
						break;
					case 2:
						str = '退货退款'
						break;
				}
				return str
			},
			// 转化下架原因
			formatDismount(num) {
				let str
				switch (num) {
					case 1:
						str = '主动下架'
						break;
					case 2:
						str = '售出下架'
						break;
					case 3:
						str = '关闭下架'
						break;
					case 4:
						str = '非法信息'
						break;						
				}
				return str
			},
			// 跳转到个人中心详情页
			linkHomePage(userId2) {
				let arg = '[{"userId2":"' + userId2 + '"}]'
				this.link('/market/manager/homePage', arg)
			},
			backAppIndex() {
				console.log('返回APP首页')
			},
			timeago(dateTimeStamp) {
				dateTimeStamp = dateTimeStamp.replace(/-/g, "/")
				var minute = 1000 * 60;//把分，时，天，周，半个月，一个月用毫秒表示
				var hour = minute * 60;
				var day = hour * 24;
				var week = day * 7;
				var halfamonth = day * 15;
				var month = day * 30;
				var now = new Date().getTime();//获取当前时间毫秒 
				var a = new Date(dateTimeStamp).getTime();

				var diffValue = now - a; //时间差
				var result = ''
				if (diffValue < 0) {
					return;
				}
				var minC = diffValue / minute;  //计算时间差的分，时，天，周，月
				var hourC = diffValue / hour;
				var dayC = diffValue / day;
				var weekC = diffValue / week;
				var monthC = diffValue / month;
				if (monthC >= 1 && monthC < 3) {
					result = parseInt(monthC) + "月前"
				} else if (weekC >= 1 && weekC <= 3) {
					result = parseInt(weekC) + "周前"
				} else if (dayC >= 1 && dayC <= 6) {
					result = parseInt(dayC) + "天前"
				} else if (hourC >= 1 && hourC <= 23) {
					result = parseInt(hourC) + "小时前"
				} else if (minC >= 1 && minC <= 59) {
					result = parseInt(minC) + "分钟前"
				} else if (diffValue >= 0 && diffValue <= minute) {
					result = "刚刚"
				} else {
					result = "3月前"
				}
				return result
			},
			// 返回上一层或者调用安卓 和IOS 方法返回上一层
			onClickLeft() {
				let popVC = this.$router.currentRoute.query.popVC;
				let backType=this.$router.currentRoute.query.backType;
				if(backType==3){
					this.link('/market/manager/index')
					return false
				}
				// 订单详情页面返回IM页面
				else if(backType==4){
					this.getIm(this.goodsId,this.nickName,this.buyerId,this.sellerId,this.goodsCoverUrl,this.savatar,this.ordersId);
					return false
				}
				// 买家退款详情页面返回到申请页面
				else if(backType==5){
                    this.closeWin() 
					return false
				}
				if (!popVC) {
					window.history.back(-1);
				} else {
					if (popVC && Browser.isIphone) {
						window.webkit.messageHandlers.popVC.postMessage({});
					} else if (popVC && Browser.isAndroid) {
						window.loadAndroid.jsGoBack()
					}
				}
			},
			// 电话验证正则		    
			regTel(str) {
				let result = true
				let reg = /^[1][3,4,5,7,8][0-9]{9}$/
				if (reg.test(str)) {
					result = true
				} else {
					result = false
				}
				return result
			},
			toFix2(num) {
				if (num) {
					return num.toFixed(2)
				} else {
					return '0.00'
				}
			},
			concatUrl(url, arg){
				let userId = this.$router.currentRoute.query.userId
                url+='?userId='+userId
                if(arg){
	                let b=JSON.parse(arg)
					for (var i = 0; i < b.length; i++) {
						let keyStr = Object.keys(b[i])[0]
						url+='&'+keyStr+'='+b[i][keyStr]
					}                 	
                }
				url='http://'+Browser.apiUrl+url
				return url
			},
			// 跳转链接
			link(url, arg) {
				let userId = this.$router.currentRoute.query.userId
				if (url == '/') {
					this.$router.push({ path: '/', query: { userId: userId } })
				} else {
					let obj = {}
					obj.t = new Date().getTime()
					obj.userId = userId
					if (arg) {
						let b = JSON.parse(arg)
						for (var i = 0; i < b.length; i++) {
							let keyStr = Object.keys(b[i])[0]
							obj[keyStr] = b[i][keyStr]
						}
					}
					this.$router.push({
						path: url,
						query: obj
					})
				}
			},
			// 跳转链接11
			links(url, arg, currentUrl) {
				let that = this;
				let userId = this.$router.currentRoute.query.userId
				if (url == '/') {
					this.$router.push({ path: '/', query: { userId: userId } })
				} else {
					let obj = {}
					obj.t = new Date().getTime()
					obj.userId = userId
					if (arg) {
						let b = JSON.parse(arg)
						for (var i = 0; i < b.length; i++) {
							let keyStr = Object.keys(b[i])[0]
							obj[keyStr] = b[i][keyStr]
						}
					}
					this.$router.replace({
						path: currentUrl
					}, function () {
						that.$router.push({
							path: url,
							query: obj
						})
					})
				}
			},
			// 跳转商品详情页
			linkGoodsDetail(userId1, userId2, arg) {
				if (userId1 == userId2) {
					this.link('/market/goods/preview', arg)
				} else {
					this.link('/market/goods/detail', arg)
				}
			}
		}
	};
	return obj;
}

// 原生跳转地址管理
export const getAppUrl = () => {
	let api = {
		'url01': "/market/goods/im",  // 我想要=》原生IM立即购买页面（安卓）
		'url02': "/market/goods/im",  // 我想要=》原生IM立即购买页面（苹果）
	}
	return api
}

export const getTimer = () => {
	let timer = {
		paymentTime: 86400,   // 最大付款时间
		receivingTime: 864000, // 最大确认收货时间
		evaluateTime: 1296000,  // 最大评价时间
		// 最大客服介入时间
	}
	return timer
}




export const apiUrl = () => {
	return 'http://'+Browser.apiUrl
	//	return 'http://192.168.10.208:8080'
}

